package com.songlanyun.modules.rabbitmq.interactmsg.impl;

import com.songlanyun.common.enums.ApplyNetRedConstant;
import com.songlanyun.common.enums.OpusConstant;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.enums.SysConstant;
import com.songlanyun.common.utils.Constant;
import com.songlanyun.common.utils.push.GeTuiUtils;
import com.songlanyun.common.utils.push.Msg;
import com.songlanyun.modules.account.entity.UserInfoEntity;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.celebrity.entity.UserApplyNetRed;
import com.songlanyun.modules.celebrity.service.UserApplyNetRedService;
import com.songlanyun.modules.message.entity.PrivateMessage;
import com.songlanyun.modules.message.model.InteractionMessage;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.message.service.PrivateMessageService;
import com.songlanyun.modules.opus.entity.Opus;
import com.songlanyun.modules.opus.service.OpusService;
import com.songlanyun.modules.rabbitmq.interactmsg.IInteractMsgService;
import com.songlanyun.modules.shop.entity.ShopApplyEntity;
import com.songlanyun.modules.shop.entity.ShopEntity;
import com.songlanyun.modules.shop.service.ShopApplyService;
import com.songlanyun.modules.shop.service.ShopService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 审核消息
 */
@Service("checkInteractService")
public class CheckInteractServiceImpl implements IInteractMsgService {

    @Resource
    private OpusService opusService;
    @Resource
    private UserApplyNetRedService userApplyNetRedService;
    @Resource
    private ShopApplyService shopApplyService;

    @Resource
    private PrivateMessageService privateMessageService;

    @Resource
    private GeTuiUtils geTuiUtils;

    @Override
    public void savePrivateMsg(MessageTO to) {
        Long modelId = to.getModelId();
        String remark = "";
        switch (to.getInteractionType()) {
            case OPUS_CHECK:
                Opus opus = opusService.getById(modelId);
                if (opus != null) {
                    OpusConstant.VerifyStatus opusStatus = opus.getOpusStatus();
                    String statusMsg = opusStatus.getMsg();
                    remark = String.format("作品【%s】审核%s", opus.getTitle(), statusMsg);
                }

                break;
            case NETRED_CHECK:
                UserApplyNetRed userApplyNetRed = userApplyNetRedService.getById(modelId);
                if (userApplyNetRed != null) {
                    ApplyNetRedConstant.StateEnum state = userApplyNetRed.getState();
                    String statusMsg = state.getMsg();
                    remark = to.getInteractionType().getMsg() + statusMsg;
                }

                break;
            case SHOP_CHECK:
            case UNION_BUSINESS_CHECK:
                ShopApplyEntity shopApply = shopApplyService.getById(modelId);
                if (shopApply != null) {
                    Integer auditStatus = shopApply.getAuditStatus();
                    String statusMsg = Constant.PASS == auditStatus ? "通过" : "不通过";
                    remark = to.getInteractionType().getMsg() + statusMsg;
                }

                break;
        }

        InteractionMessage interactionMessage = new InteractionMessage(to.getInteractionType(), to.getModelId(), remark);
        PrivateMessageTO privateMessageTO = new PrivateMessageTO(to.getUserId(), PrivateMessageConstant.MessageType.INTERACTION_MESSAGE, interactionMessage, remark);

        PrivateMessage privateMessage = new PrivateMessage(privateMessageTO);
        privateMessageService.save(privateMessage);

        //单推
        geTuiUtils.pushByUserId(
                Msg.builder()
                        .uid(to.getUserId())
                        .title(PrivateMessageConstant.MessageType.INTERACTION_MESSAGE.getMsg())
                        .content(remark)
                        .msg(privateMessage.pushMessageBody())
                        .build()
        );
    }
}
